package com.example.helllo;
//自定义Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyAdapter extends ArrayAdapter {
    public MyAdapter(@NonNull Context context, int resource, @NonNull ArrayList<HashMap<String,String>> list) {
        super(context, resource, list);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {//convertView是当前行的显示，parent是list控件
        View itemView = convertView;//用于显示每一行
        if(itemView==null){
            //从布局文件获得对象，对布局初始化。false为不覆盖根布局，不然就会一行占整个页面
            itemView= LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }
        //获得map对象，获得当前行数据
        Map<String,String> map =(Map<String,String>)getItem(position);
        TextView title = (TextView)itemView.findViewById(R.id.itemTitle);//从布局中找控件和控件绑定，并重新取名
        TextView detail = (TextView)itemView.findViewById(R.id.itemDetail);

        title.setText("T:"+map.get("Country"));//设置文本内容
        detail.setText("D:"+map.get("Rate"));
        return itemView;
        //根据不同的position返回不同的itemView，可实现自定义多级
    }
}
